package com.smart.terminal.controller;

import com.smart.aop.log.LogType;
import com.smart.aop.log.SaveLog;
import com.smart.aop.permission.HasPermission;
import com.smart.common.utils.EncodeUtil;
import com.smart.entity.terminal.SshEntity;
import com.smart.model.response.r.Result;
import com.smart.model.terminal.SshRequestVO;
import com.smart.service.terminal.SshService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;


/**
 * 终端 Controller
 *
 * @author wf
 * @since 2022-11-08 10:52:46
 */
@RestController
@RequestMapping("/terminal/ssh")
public class SshController {
    @Autowired
    SshService sshService;

    /**
     * 列表
     *
     * @param sshEntity 终端bean
     * @return String
     */
    @GetMapping("/page")
    public String page(SshEntity sshEntity) {
        return Result.data(sshService.findPage(sshEntity));
    }

    /**
     * 信息
     *
     * @param id 主键ID
     * @return String
     */
    @GetMapping("/get")
    public String get(@RequestParam String id) {
        SshEntity result = sshService.get(id);
        return result != null ? Result.data(result) : Result.fail();
    }

    /**
     * 保存
     *
     * @param sshEntity 终端bean
     * @return String
     */
    @HasPermission("ssh:add")
    @PostMapping("/save")
    @SaveLog(module = "终端管理", type = LogType.ADD)
    public String save(@RequestBody SshEntity sshEntity) {
        SshEntity result = sshService.saveEntity(sshEntity);
        return result != null ? Result.data(result) : Result.fail();
    }

    /**
     * 修改
     *
     * @param sshEntity 终端bean
     * @return String
     */
    @HasPermission("ssh:update")
    @PostMapping("/update")
    @SaveLog(module = "终端管理", type = LogType.UPDATE)
    public String update(@RequestBody SshEntity sshEntity) {
        SshEntity result = sshService.updateEntity(sshEntity);
        return result != null ? Result.data(result) : Result.fail();
    }

    /**
     * 删除
     *
     * @param sshEntity 终端bean
     * @return String
     */
    @HasPermission("ssh:delete")
    @PostMapping("/delete")
    @SaveLog(module = "终端管理", type = LogType.DELETE)
    public String delete(@RequestBody SshEntity sshEntity) {
        return Result.status(sshService.delete(sshEntity));
    }

    /**
     * 列表（不分页）
     *
     * @param sshEntity 终端bean
     * @return String
     */
    @GetMapping("/list")
    public String list(SshEntity sshEntity) {
        return Result.data(sshService.findList(sshEntity));
    }

    /**
     * 上传文件
     */
    @PostMapping("/upload")
    public String upload(SshRequestVO sshRequestVO) {
        sshService.upload(sshRequestVO);
        return Result.success();
    }

    /**
     * 下载文件
     */
    @PostMapping("/download")
    public void download(@RequestBody SshRequestVO sshRequestVO, HttpServletResponse response) {
        InputStream in = null;
        OutputStream out = null;
        try {
            in = sshService.download(sshRequestVO);
            int len = 0;
            byte[] buffer = new byte[1024];
            out = response.getOutputStream();
            response.reset();
            response.addHeader("Content-Disposition", "attachment; filename=" + EncodeUtil.encodeUrl(sshRequestVO.getFileName()));
            response.setContentType("application/octet-stream");
            while ((len = in.read(buffer)) > 0) {
                out.write(buffer, 0, len);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 关闭输入流
            if (in != null) {
                try {
                    in.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            // 关闭输出流
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 获取文件目录
     *
     * @param sshRequestVO 请求模型
     * @return String
     */
    @PostMapping("/dir")
    public String dir(@RequestBody SshRequestVO sshRequestVO) {
        return Result.data(sshService.dir(sshRequestVO));
    }

    /**
     * 获取文件目录
     *
     * @param sshRequestVO 请求模型
     * @return String
     */
    @PostMapping("/del")
    public String del(@RequestBody SshRequestVO sshRequestVO) {
        sshService.del(sshRequestVO);
        return Result.success();
    }
}
